<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>

<body>
    <div id="app">
        <div class="container">
            <h1>{{title}}</h1>
            <table v-if="goods.length" class="table table-hover">
                <thead>
                    <tr>
                        <th>
                            <input type="checkbox" @click="allCheck" :checked="allChecked" id="">
                            全选
                        </th>
                        <th>商品</th>
                        <th>价格</th>
                        <th>数量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(item,index) in goods">
                        <td><input type="checkbox" :checked="item.iscreat" @click="mycheck(index)" id=""></td>
                        <td>{{item.title}}</td>
                        <td>{{item.price}}</td>
                        <td>{{item.num}}</td>
                        <td><button @click="mydelete(index)">删除</button></td>
                    </tr>
                </tbody>
            </table>
            <div v-else>
                <h2>购物车空空如也</h2>
            </div>
        </div>
    </div>
</body>
<script src="./vue.min.js"></script>
<script>
    let vm = new Vue({
        el: "#app",
        data: {
            title: "购物车",
            allChecked: false,
            goods: [
                {
                    title: "鞋",
                    num: 1,
                    price: 20,
                    iscreat: false
                },
                {
                    title: "手机",
                    num: 1,
                    price: 100,
                    iscreat: true
                },
                {
                    title: "帽子",
                    num: 1,
                    price: 10,
                    iscreat: false
                }
            ]

        },
        methods: {
            // 全选
            allCheck() {
                this.allChecked = !this.allChecked
                this.goods.forEach(item => {
                    item.iscreat = this.allChecked;
                });
            },
            //单选
            mycheck(index){
                this.goods[index].iscreat = !this.goods[index].iscreat
                let res =  this.goods.every(item=>{
                    return item.iscreat == true;
                })
                this.allChecked = res;
            },
            //删除
            mydelete(index){
            //   let res =  this.goods.filter((item,i)=>{
            //         if(i != index){
            //             return item;
            //         }
            //   })
            //   this.goods = res;
              this.goods.splice(index,1)
            }

        },
    })
</script>

</html>